package cn.xiaopengstack.domain.trade.rule.factory;

import cn.xiaopengstack.domain.activity.model.entity.GroupBuyActivityEntity;
import cn.xiaopengstack.domain.trade.rule.trade.ActivityAbilityFilter;
import cn.xiaopengstack.domain.trade.rule.trade.EndNodeLockFilter;
import cn.xiaopengstack.domain.trade.rule.trade.TradeStockLockFilter;
import cn.xiaopengstack.domain.trade.rule.trade.UserTakeLimitRuleFilter;
import cn.xiaopengstack.domain.trade.model.entity.TradeRuleCommandEntity;
import cn.xiaopengstack.domain.trade.model.entity.TradeRuleResponseEntity;
import cn.xiaopengstack.types.framework.strategy.link.mutilModel.BusinessLinkedList;
import cn.xiaopengstack.types.framework.strategy.link.mutilModel.LinkArmory;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class TradeRuleFilterFactory {


    /**
     * 组装规则链
     * @param activityAbilityFilter
     * @param userTakeLimitRuleFilter
     * @param tradeStockLockFilter
     * @param endNodeLockFilter
     * @return
     */
    @Bean("tradeRuleValidateFilter")
    public BusinessLinkedList<TradeRuleCommandEntity, DynamicContext, TradeRuleResponseEntity> tradeRuleFilter(ActivityAbilityFilter activityAbilityFilter,
                                                                                                               UserTakeLimitRuleFilter userTakeLimitRuleFilter,
                                                                                                               TradeStockLockFilter tradeStockLockFilter,
                                                                                                               EndNodeLockFilter endNodeLockFilter){
        LinkArmory<TradeRuleCommandEntity, DynamicContext, TradeRuleResponseEntity> linkArmory = new LinkArmory<>("交易规则校验过滤链",
                activityAbilityFilter,
                userTakeLimitRuleFilter,
                tradeStockLockFilter,
                endNodeLockFilter
                );
        return linkArmory.getLogicLink();
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class DynamicContext{


        private GroupBuyActivityEntity groupBuyActivityEntity;

        /**
         * 当前用户在当前活动已参与数量
         */
        private Integer userTakeOverActivityCount;
    }
}
